#include "../common/Log.hpp"
#include "../common/util.hpp"
#include <mysql/mysql.h>

using namespace util_ns;
using namespace LogModule_ns;
using namespace std;
using namespace LogModule_ns;
using namespace util_ns;

class QuestionDescribe
{
public:
    using ptr = std::shared_ptr<QuestionDescribe>;

    ~QuestionDescribe() = default;
    std::string id;
    std::string title;
    std::string star;
    int cpu_load;
    int mem_load;
    std::string desc;
    std::string code;
    std::string tail;
};

const std::string oj_questions = "oj_questions";
const std::string host = "123.56.180.103";
const std::string user = "oj_client";
const std::string passwd = "123";
const std::string db = "oj_client";
const int port = 3306;

class QuestionsModule
{
public:
    QuestionsModule()
    {
    }
    bool QueryMySql(const std::string &sql, std::vector<QuestionDescribe::ptr> &out)
    {
        // 创建mysql句柄
        MYSQL *my = mysql_init(nullptr);

        // 连接数据库
        if (nullptr == mysql_real_connect(my, host.c_str(), user.c_str(), passwd.c_str(), db.c_str(), port, nullptr, 0))
        {
            DLOG(FATAL) << "连接数据库失败!" << "\n";
            return false;
        }

        // 一定要设置该链接的编码格式, 要不然会出现乱码问题
        mysql_set_character_set(my, "utf8");

        DLOG(INFO) << "连接数据库成功!" << "\n";

        // 执行sql语句
        if (0 != mysql_query(my, sql.c_str()))
        {
            DLOG(WARNING) << sql << " execute error!" << "\n";
            return false;
        }

        // 提取结果
        MYSQL_RES *res = mysql_store_result(my);

        // 分析结果
        int rows = mysql_num_rows(res);   // 获得行数量
        int cols = mysql_num_fields(res); // 获得列数量

        for (int i = 0; i < rows; i++)
        {
            QuestionDescribe::ptr q = std::make_shared<QuestionDescribe>();
            MYSQL_ROW row = mysql_fetch_row(res);
            q->id = row[0];
            q->title = row[1];
            q->star = row[2];
            q->desc = row[3];
            q->code = row[4];
            q->tail = row[5];
            q->cpu_load = atoi(row[6]);
            q->mem_load = atoi(row[7]);
            // std::cout << "query result : " << q->id << std::endl;
            out.push_back(q);
        }
        // 释放结果空间
        mysql_free_result(res);
        // 关闭mysql连接
        mysql_close(my);

        return true;
    }
    bool GetAllQuestions(std::vector<QuestionDescribe::ptr> &out)
    {
        std::string sql = "select * from ";
        sql += oj_questions;
        return QueryMySql(sql, out);
    }
    bool GetOneQuestion(const std::string &number, QuestionDescribe::ptr &q)
    {
        bool res = false;
        std::string sql = "select * from ";
        sql += oj_questions;
        sql += " where id = ";
        sql += number;
        std::vector<QuestionDescribe::ptr> result;
        if (QueryMySql(sql, result))
        {
            if (result.size() == 1)
            {
                q = result[0];
                res = true;
            }
        }
        return res;
    }
    ~QuestionsModule()
    {
    }
};
